package com.qeebu.SMSManagerClient.smsList;

import java.util.ArrayList;
import java.util.List;

import com.qeebu.SMSManagerClient.base.BaseActivity;
import com.qeebu.SMSManagerClient.R;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 每个联系人对话的短信列表
 * @author yongjun
 *
 */
public class PersonSMSListActivity extends BaseActivity  implements AdapterView.OnItemClickListener{
    private ListView sms_detail_list;
    //private SMSDetailAdapter dialogAdapter;
    private TextView sms_people_name;
    private List<PersonSMSListItem> smsList = new ArrayList<PersonSMSListItem>();
    private View progressBar;
    private TextView msgBody;
    private String msgStr;
    private ScrollView scrollView;
    
    public static final Uri MMSSMS_FULL_CONVERSATION_URI = Uri.parse("content://mms-sms/conversations/");
    public static final Uri CONVERSATION_URI = MMSSMS_FULL_CONVERSATION_URI.buildUpon().appendQueryParameter("simple", "true").build();
    public static String sms_id;
    
    private SMSPersonListAdapter dialogAdapter;

    public List<PersonSMSListItem> getSmsList()
    {
        return smsList;
    }

    public void setSmsList(List<PersonSMSListItem> smsList)
    {
        this.smsList = smsList;
    }
        
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_list_detail_layout);
        sms_people_name = (TextView)findViewById(R.id.sms_people_name);           //该组短信的发送接收者
        sms_detail_list = (ListView)findViewById(R.id.sms_detail_list);           //短信列表
        progressBar = (View)findViewById(R.id.smslist_progress_bar);
        scrollView = (ScrollView)findViewById(R.id.sms_body_scrl_view);
        msgBody = (TextView)findViewById(R.id.sms_body);
        progressBar.setVisibility(View.VISIBLE);
        
        msgStr = getResources().getString(R.string.sms_content_hint);
        msgBody.setText(msgStr);
        
        Intent arg = this.getIntent();
        Bundle bundle = arg.getExtras();
        Long id = bundle.getLong("id");
        String name = bundle.getString("name");
        
        sms_people_name.setText(name);
        
        ReadPersonSMSAsyncTask smsReader = new ReadPersonSMSAsyncTask(PersonSMSListActivity.this, id.toString());
        smsReader.execute(null);
        
        dialogAdapter = new SMSPersonListAdapter(this);
        sms_detail_list.setOnItemClickListener(this);
        sms_detail_list.setAdapter(dialogAdapter);
    }
    
    public void setAdapter(){
        sms_detail_list.setAdapter(dialogAdapter);
    }
    
    public View getProgressBar()
    {
        return progressBar;
    }

    public void setProgressBar(View progressBar)
    {
        this.progressBar = progressBar;
    }

    class PersonalSMSViewHold{
        TextView date;
        TextView smsBody;
    }
    
    class SMSPersonListAdapter extends BaseAdapter{
        private Context context;
        
        public SMSPersonListAdapter(Context ctx){
            context = ctx;
        }
        
        public View getView(int position, View convertView, ViewGroup parent)
        {
            PersonalSMSViewHold viewHold;
            
            if(convertView == null){
                LayoutInflater inflater = LayoutInflater.from(context);
                convertView = inflater.inflate(R.layout.sms_list_detail_item, null);
                viewHold = new PersonalSMSViewHold();
                viewHold.date = (TextView)convertView.findViewById(R.id.sms_recvorsend_date);
                viewHold.smsBody = (TextView)convertView.findViewById(R.id.detail_sms_body);
                convertView.setTag(viewHold);
            }else{
                viewHold = (PersonalSMSViewHold)convertView.getTag();
            }
            //scrollView.setVisibility(View.GONE);
            sms_detail_list.invalidate();
            viewHold.date.setText(smsList.get(position).getDate());
            viewHold.smsBody.setText(smsList.get(position).getBody());
            //convertView.setTag(viewHold);
            return convertView;
        }
        
        public int getCount()
        {
            if (smsList == null || smsList.isEmpty())
            {
                return 0;
            }
            return smsList.size();
        }

        public Object getItem(int position)
        {
            try
            {
                return smsList.get(position);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                return null;
            }
        }

        public long getItemId(int position)
        {
            return position;
        }
    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3)
    {
        scrollView.setVisibility(View.VISIBLE);
        msgBody.setText(msgStr + smsList.get(pos).getBody());
    }
}
